<%-- 
    Document   : index
    Created on : Jun 2, 2011, 11:32:35 AM
    Author     : C-12®
--%>
<%if (null != session.getAttribute("auth")) {
        response.sendRedirect(request.getContextPath() + "/view.jsp");
    }
%>
<%@ page import="control.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Page</title>

    </head>
    <body>

        <h1>Login Page!</h1>
        <form method="post" action="index.jsp">
				Username:<br /><input type="text" name="username" /><br />
				Password:<br /><input type="password" name="password" /><br />
            <input type="submit" value="Login" /><br />
            <p></p>
        </form>
        <%

            String username = request.getParameter("username");
            String pass = request.getParameter("password");
            String context = request.getContextPath();
            if (username != null) {
                if (!username.isEmpty()
                        && !pass.isEmpty()) {
                    AccessClass d = new AccessClass();
                    if (d.loadDB()) {
                        if (d.auth(username, pass)) {
                            session.setAttribute("uname", username);
                            session.setAttribute("pwd", pass);
                            session.setAttribute("auth", "logged");
                            response.sendRedirect(context + "/view.jsp");

                        } else {
        %><span style="font-weight: bold; color: red">Wrong username or password.</span><%}
        } else {
        %><span style="font-weight: bold; color: red">Connection problem.</br>Please try again later.</span><%                    }
                }
            }

            %>

    </body>
</html>
